//  This file is part of Swordbible - http://www.swordwarrior.net
//  Copyright (C) 2003-2009 Manu Mital mital.manu@gmail.com

//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2, or (at your option)
//  any later version.

//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.

//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software Foundation,
//  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.


// DevotionalPage.cpp : implementation file
//

#include <stdafx.h>
#include "SwordBible.h"
#include "SwordBibleDoc.h"
#include "DevotionalPage.h"
#include "BiblePage.h"
#include ".\devotionalpage.h"
#include "BibleForm.h"


int CDevotionalPage::staticPageCount = 0;  // number of pages created
int CDevotionalPage::active = 0;

CSwordBibleFonts CDevotionalPage::font;
// CDevotionalPage dialog

IMPLEMENT_DYNAMIC(CDevotionalPage, CPropertyPage)
CDevotionalPage::CDevotionalPage()
	: CPropertyPage(CDevotionalPage::IDD)
	, m_devotionStartup(FALSE)
{
	
	staticPageCount++;				// increment the instance counter
	pModule = NULL;
	month = 1;
	day = 1;
   	
}

CDevotionalPage::~CDevotionalPage()
{
	staticPageCount--;
			
}

void CDevotionalPage::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	DDX_Check(pDX, IDC_DEVOTION_STARTUP, m_devotionStartup);
	DDX_Control(pDX, IDC_RICHEDIT_DEVOTIONAL, m_devotionalRich);
	DDX_Control(pDX, IDC_DD_CALENDAR, DDCalendarCtrl);
	DDX_Control(pDX, IDC_DD_CRYSTAL, CrystalCal);
	DDX_Control(pDX, IDC_RICHEDIT_DD_DATE, m_richeditDate);
}


BEGIN_MESSAGE_MAP(CDevotionalPage, CPropertyPage)
	ON_COMMAND(ID_DEVOTIONAL_PRINT,OnPrint)
	ON_COMMAND(ID_DEVOTIONAL_SELECTALL, OnSelectAll)
	ON_COMMAND(ID_DEVOTIONAL_COPY, OnDevotionalCopy)
	ON_COMMAND(ID_DEVOTIONAL_ZOOM, OnZoom)
	ON_UPDATE_COMMAND_UI(ID_DEVOTIONAL_ZOOM, OnUpdateZoom)
	ON_COMMAND(ID_DEVOTIONAL_DISPLAY, OnDevotionalDisplay)
	ON_COMMAND(ID_PREVIOUSDATE,OnPreviousDate)
	ON_COMMAND(ID_NEXTDATE,OnNextDate)
	ON_COMMAND(ID_DEVOTIONAL_VERSIONS, OnShowVersions)
	ON_WM_SIZE()
	ON_BN_CLICKED(IDC_DEVOTION_STARTUP, OnBnClickedDevotionStartup)

	ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTW, 0, 0xFFFF, OnToolTipNotify)
   ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTA, 0, 0xFFFF, OnToolTipNotify)

    ON_NOTIFY(EN_MSGFILTER, IDC_RICHEDIT_DEVOTIONAL, OnEnMsgFilter)
   ON_NOTIFY(MCN_SELCHANGE, IDC_DD_CALENDAR, &CDevotionalPage::OnSelDDCalendar)
   ON_EN_SETFOCUS(IDC_RICHEDIT_DD_DATE, &CDevotionalPage::OnFocusRicheditDate)
END_MESSAGE_MAP()


// CDevotionalPage message handlers

void CDevotionalPage::OnSize(UINT nType, int cx, int cy)
{
	CPropertyPage::OnSize(nType, cx, cy);
	
	// postion the startup option check box
	CWnd * checkWnd = GetDlgItem(IDC_DEVOTION_STARTUP);

		if(checkWnd->GetSafeHwnd())
        checkWnd->SetWindowPos(NULL, 10, cy - 17, 250,14,
		SWP_NOZORDER | SWP_NOACTIVATE);


	if(DDCalendarCtrl.GetSafeHwnd())
	DDCalendarCtrl.SetWindowPos(NULL,cx - 180,40,174,154,SWP_NOZORDER | SWP_NOACTIVATE);

	if(m_devotionalRich.GetSafeHwnd()) 
	m_devotionalRich.SetWindowPos(NULL,10,40,cx-200,cy-63,SWP_NOZORDER | SWP_NOACTIVATE);

	pt.x = cx - 148; pt.y = 215;

	if(m_richeditDate.GetSafeHwnd()) 
	m_richeditDate.SetWindowPos(NULL,pt.x + 1, pt.y + 38,102 ,70 ,SWP_NOZORDER | SWP_NOACTIVATE);

	if(CrystalCal.GetSafeHwnd()) 
	CrystalCal.SetWindowPos(NULL,pt.x, pt.y ,108,119 ,SWP_NOZORDER | SWP_NOACTIVATE);

	// to prevent fluctuation, we redraw only the relevant portion of the screen
	InvalidateRect( CRect(pt.x - 5, pt.y -5, pt.x + 115, pt.y + 125));
		

}

BOOL CDevotionalPage::OnInitDialog()
{
	CPropertyPage::OnInitDialog();

	if(CSwordBibleDoc::numDevAvailable > 0) 
	{
		active = 0;
		pModule = CSwordBibleDoc::pRTFDevModule[active];
	}

	font.location = AFX_DEVOTIONAL;
	font.Load();
	font.ConstructRTF();

		//calculate the width and height of chars
	TEXTMETRIC tm;
	CClientDC dc(GetDlgItem(IDC_RICHEDIT_DEVOTIONAL));
	dc.GetTextMetrics(&tm);
	font.text_cx = tm.tmAveCharWidth;
	font.text_cy = tm.tmHeight + tm.tmExternalLeading;


    // set the check in the startup option check box
	 m_devotionStartup = CSwordBibleDoc::devotion_startup;
	UpdateData(FALSE);



	CTime time;
	DDCalendarCtrl.GetToday(time);
	
	// set the key
	month  = time.GetMonth();
	day = time.GetDay();
	

	// set the key
	CString key;
	
	if(month > 9 && day > 9)
	key.Format(_T("%d.%d"),month, day);

	else if(month < 10 && day < 10)
	key.Format(_T("0%d.0%d"),month, day);

	else if (month > 9 && day < 10)
	key.Format(_T("%d.0%d"),month, day);

	else if (month < 10 && day > 9)
	key.Format(_T("0%d.%d"),month, day);

	CSwordBibleDoc::devotionKey = (CStringA) key;


	
	// show devotinal text
	 //showText();  <---- not needed,  called by OnSetActive()
	
	m_devotionalRich.SetEventMask(ENM_MOUSEEVENTS | ENM_KEYEVENTS | ENM_SCROLLEVENTS);
	m_devotionalRich.SendMessage(EM_EXLIMITTEXT,0,-1);
   
	
	 if (!m_wndFormatBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD
       | WS_VISIBLE | CBRS_TOP |  CBRS_FLYBY | CBRS_TOOLTIPS) ||
       !m_wndFormatBar.LoadToolBar(IDR_DEVOTIONAL_TOOLBAR))
   {
      TRACE0("Failed to create toolbar\n");
      return -1;      // fail to create
   }

	m_wndFormatBar.LoadTCToolBar(24, IDB_TOOLBAR_DEVOTION,IDB_TOOLBAR_DEVOTION,
                              IDB_TOOLBAR_DEVOTION, RGB(255,255,255));


	// Position the control bars using RepositionBars. 
	RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0);

	// Expand the size of the Toolbar window so all controls in it would fit
	

	// Expand the size of the Toolbar window so all controls in it would fit	
	CRect toolBarRect;
	CToolBarCtrl& formatBar = m_wndFormatBar.GetToolBarCtrl();
	formatBar.GetWindowRect(toolBarRect);
	ScreenToClient(toolBarRect);
	toolBarRect.left = 10;
	toolBarRect.right += 600;
	formatBar.MoveWindow(toolBarRect,TRUE);


	
    EnableToolTips();   // enable tool tips for view
 

	// diable the 10 ID_PLACEHOLDER BUTTONS
	for(int n = 0; n < 10; n++)
	formatBar.SendMessage((UINT) TB_ENABLEBUTTON, (WPARAM)(int) ID_PLACEHOLDER1 + n,  (LPARAM) MAKELONG(FALSE, 0) ); 

	
  

    return FALSE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}



// displays the text in the rich edit box of the devotional
void CDevotionalPage::showText()
{
	
    
	if(!pModule) return;

	pModule->SetKey(CSwordBibleDoc::devotionKey);

	
        CString chapText = font.GetHeaderRTF();
		

		CString titleSize;
		titleSize.Format(_T("\\cf2\\fs%d "), font.text_facesize + 5);
		chapText = chapText +  titleSize + (CString) pModule->Description();
		
		// reset the font size
		titleSize.Format(_T("\\fs%d"), font.text_facesize);
		chapText = chapText + titleSize + _T("\\par\\par ");
							
		chapText = chapText  + (CString) pModule->RenderText() + _T("}"); 
			
		if(CSwordBibleDoc::crossref_tags)
		{
		chapText.Replace(_T("<a href=\"\">"),  _T("<a href=\"\">\\cf6\\b "));
		chapText.Replace(_T("</a>"), _T("</a>\\cf2\\b0 "));
		}

		else
		{
			chapText.Replace(_T("<a href=\"\">"),  _T(""));
			chapText.Replace(_T("</a>"), _T(""));
		}


		m_devotionalRich.SetRTF(chapText);

		tips.Clear();
		if(CSwordBibleDoc::crossref_tags) CreateTips();

		m_devotionalRich.SetSel(0,0);
		m_devotionalRich.SetFocus();

		
		ZoomText();


		// change the date on the calendar picture
		CString picDate;
		picDate.Format(_T("{\\rtf1{\\fonttbl {\\f1 Arial;}}\\f1\\fs22\\b %s \\par\\fs60 %d \\b0 }"), 
			(LPCTSTR) CSwordBibleDoc::monthNames[month - 1], day);
		m_richeditDate.SetRTF(picDate);
}


void CDevotionalPage::OnShowVersions()
{


	RECT rc;
    m_wndFormatBar.GetToolBarCtrl().GetItemRect(0,&rc);
	m_wndFormatBar.ClientToScreen(&rc); // TrackPopupMenu uses screen coords

   CMenu menu;
   menu.CreatePopupMenu();
   CMenu* pPopup = NULL;
   pPopup = &menu;

   if(pPopup == NULL) return;
   int dwSelectionMade;  

 
   // fill in the strings for different bible versions
	for(int i = 0; i < CSwordBibleDoc::numDevAvailable; i++)
		if(CSwordBibleDoc::pRTFDevModule[i] != NULL)
		{
			         pPopup->AppendMenu(MF_STRING,ID_DEVOTIONAL_NAMES + i, (CString) CSwordBibleDoc::pRTFDevModule[i]->Description());
					 if(active == i) pPopup->CheckMenuItem(ID_DEVOTIONAL_NAMES + i,MF_CHECKED); 

		}


         dwSelectionMade =  pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON|TPM_RETURNCMD, 
         rc.left, rc.bottom + 4, this);

	
		int selection = dwSelectionMade;
		int base = ID_DEVOTIONAL_NAMES;
		int temp = selection - base;


	if(temp >= 0 && temp < CSwordBibleDoc::numDevAvailable) 
	{
		active = temp;
	     pModule = CSwordBibleDoc::pRTFDevModule[active];
	     showText();
		
	}

    pPopup->DestroyMenu(); 


}






void CDevotionalPage::CreateTips()
{
	
	
	FINDTEXTEX ft1, ft2;
	ft1.chrg.cpMin = 0;
	ft1.chrg.cpMax = -1;
	ft1.lpstrText = _T("<a href=\"\">");

	ft2.chrg.cpMin = 0;
	ft2.chrg.cpMax = -1;
	ft2.lpstrText = _T("</a>");

	int start = 0;
	int end = 0;


	while (true) {

	
	start = m_devotionalRich.FindText(FR_DOWN , &ft1); //find the href start tags  
	end = m_devotionalRich.FindText(FR_DOWN , &ft2); //find the href tags  

	if(start == -1 || end == -1) return;

	CString ref = _T("");
	m_devotionalRich.GetTextRange(start + 11, end, ref);
	tips.Add(start, end - 11, ref);

	ft1.chrg.cpMin = start;
	ft2.chrg.cpMin = start;

	m_devotionalRich.SetSel(end, end + 4);
	m_devotionalRich.ReplaceSel(_T(""));

	m_devotionalRich.SetSel(start,start + 11);
	m_devotionalRich.ReplaceSel(_T(""));

	}


}



void CDevotionalPage::OnBnClickedDevotionStartup()
{
	// TODO: Add your control notification handler code here
		// TODO: Add your control notification handler code here
	UpdateData();
	CSwordBibleDoc::devotion_startup = m_devotionStartup;
    AfxGetApp()->WriteProfileInt(_T("Devotional"),_T("Startup"),m_devotionStartup);
}



BOOL CDevotionalPage::OnToolTipNotify(UINT id, NMHDR *pNMHDR,   LRESULT *pResult)
{
   ASSERT(pNMHDR->code == TTN_NEEDTEXTA || pNMHDR->code == TTN_NEEDTEXTW);

     // if there is a top level routing frame then let it handle the message
     if (GetRoutingFrame() != NULL) return FALSE;

     // to be thorough we will need to handle UNICODE versions of the message also !!
     TOOLTIPTEXTA* pTTTA = (TOOLTIPTEXTA*)pNMHDR;
     TOOLTIPTEXTW* pTTTW = (TOOLTIPTEXTW*)pNMHDR;
     TCHAR szFullText[512];
     CString strTipText;
     UINT nID = pNMHDR->idFrom;

     if (pNMHDR->code == TTN_NEEDTEXTA && (pTTTA->uFlags & TTF_IDISHWND) ||
         pNMHDR->code == TTN_NEEDTEXTW && (pTTTW->uFlags & TTF_IDISHWND))
     {
          // idFrom is actually the HWND of the tool 
          nID = ::GetDlgCtrlID((HWND)nID);
     }

     if (nID != 0) // will be zero on a separator
     {
          AfxLoadString(nID, szFullText);
          strTipText=szFullText;

#ifndef _UNICODE
          if (pNMHDR->code == TTN_NEEDTEXTA)
          {
               lstrcpyn(pTTTA->szText, strTipText, sizeof(pTTTA->szText));
          }
          else
          {
               _mbstowcsz(pTTTW->szText, strTipText, sizeof(pTTTW->szText));
          }
#else
          if (pNMHDR->code == TTN_NEEDTEXTA)
          {
               _wcstombsz(pTTTA->szText, strTipText,sizeof(pTTTA->szText));
          }
          else
          {
               lstrcpyn(pTTTW->szText, strTipText, sizeof(pTTTW->szText));
          }
#endif

          *pResult = 0;

          // bring the tooltip window above other popup windows
          ::SetWindowPos(pNMHDR->hwndFrom, HWND_TOP, 0, 0, 0, 0,SWP_NOACTIVATE|
               SWP_NOSIZE|SWP_NOMOVE|SWP_NOOWNERZORDER); return TRUE;
     }
      return FALSE;
}



void CDevotionalPage::OnPrint()
{
		// only the active page should handle the message
  if(  !( ((CPropertySheet*) GetParent())->GetActivePage()   == this) )  
	  return;


    CBibleForm::pControl = (CRichEditCtrl *) GetDlgItem(IDC_RICHEDIT_DEVOTIONAL);
	AfxGetMainWnd()->SendMessageToDescendants(WM_PRINT_CONTROL, (WPARAM) 1 , 0);
  
	
}

void CDevotionalPage::OnSelectAll(void) 
{
			// only the active page should handle the message
  if(  !( ((CPropertySheet*) GetParent())->GetActivePage()   == this) )  
	  return;

    m_devotionalRich.SetFocus();
    m_devotionalRich.SetSel(0,-1);


}


void CDevotionalPage::OnDevotionalCopy(void) 
{
	// only the active page should handle the message
  if(  !( ((CPropertySheet*) GetParent())->GetActivePage()   == this) )  
	  return;

  m_devotionalRich.Copy();

}

void CDevotionalPage::OnZoom()
{
	// only the active page should handle the message
		if(  !( ((CPropertySheet*) GetParent())->GetActivePage()   == this) )  
	  return;


		CSwordBibleDoc::devZoom = !(CSwordBibleDoc::devZoom);

	ZoomText();

	// set or remove the check
	m_wndFormatBar.SendMessage((UINT) TB_CHECKBUTTON, (WPARAM)(int) ID_DEVOTIONAL_ZOOM, 
		(LPARAM) MAKELONG(CSwordBibleDoc::devZoom, 0) );


}

// this function is never called 

void CDevotionalPage::OnUpdateZoom(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck(CSwordBibleDoc::devZoom);
    
		
}




void CDevotionalPage::ZoomText()
{
	if(CSwordBibleDoc::devZoom) m_devotionalRich.SendMessage(EM_SETZOOM, 5,4); //zoom by 1.25
	else m_devotionalRich.SendMessage(EM_SETZOOM, 0,0);  // turn off zooming

}



void CDevotionalPage::OnDevotionalDisplay(void)
{

	// only the active page should handle the message
  if(     ((CPropertySheet*) GetParent())->GetActivePage()   != this) return;

  CColorMgr colordlg(NULL, &font);
	
  if(colordlg.DoModal() == IDOK) {
	  m_devotionalRich.SetBackgroundColor(false, CColorMgr::RTFtoCOLORREF(font.bg_color) );
	  showText();
  }

  

	
}

void CDevotionalPage::OnPreviousDate()
{
	CTime time;
    DDCalendarCtrl.GetCurSel(time);         // Get current time
	time = time - CTimeSpan(1,0,0,0);
	DDCalendarCtrl.SetCurSel(time); // set new time

	 // set the key
	CString key;
	month  = time.GetMonth();
	day = time.GetDay();

	if(month > 9 && day > 9)
	key.Format(_T("%d.%d"),month, day);

	else if(month < 10 && day < 10)
	key.Format(_T("0%d.0%d"),month, day);

	else if (month > 9 && day < 10)
	key.Format(_T("%d.0%d"),month, day);

	else if (month < 10 && day > 9)
	key.Format(_T("0%d.%d"),month, day);

	CSwordBibleDoc::devotionKey = (CStringA) key;

	  showText();   // update display

}

void CDevotionalPage::OnNextDate()
{

	CTime time;
    DDCalendarCtrl.GetCurSel(time);         // Get current time
	time = time + CTimeSpan(1,0,0,0);
	DDCalendarCtrl.SetCurSel(time); // set new time

	 // set the key
	CString key;
	month  = time.GetMonth();
	day = time.GetDay();

	if(month > 9 && day > 9)
	key.Format(_T("%d.%d"),month, day);

	else if(month < 10 && day < 10)
	key.Format(_T("0%d.0%d"),month, day);

	else if (month > 9 && day < 10)
	key.Format(_T("%d.0%d"),month, day);

	else if (month < 10 && day > 9)
	key.Format(_T("0%d.%d"),month, day);

	CSwordBibleDoc::devotionKey = (CStringA) key;

	  showText();   // update display

}
void CDevotionalPage::OnEnMsgFilter(NMHDR *pNMHDR, LRESULT *pResult)
{
	MSGFILTER *pMsgFilter = reinterpret_cast<MSGFILTER *>(pNMHDR);


	// capture mouse move messages and check if the mouse lies within the rectangle
	// of a scripture reference. If so, show the hand sign
	if(pMsgFilter->msg == WM_MOUSEMOVE )
	{
		CPoint point;                                            
         ::GetCursorPos(&point); //where is the mouse?

		 // change the coordinates of the point from Screen to that of richedit
		m_devotionalRich.ScreenToClient(&point);

		int nchar = m_devotionalRich.CharFromPos(point);

		CString keylist = _T("");
		int lower = 0, upper = 0;
		if(tips.FromPos(nchar,lower, upper, keylist) == TRUE)
		SetCursor(CBibleSheet::m_hHandCursor);

	}
	
	
	if(pMsgFilter->msg == WM_LBUTTONDOWN)
	{ 
		
		 CPoint point;                                            
         ::GetCursorPos(&point); //where is the mouse?

		 // change the coordinates of the point from Screen to that of richedit
		m_devotionalRich.ScreenToClient(&point);

		int nchar = m_devotionalRich.CharFromPos(point);

		CString keylist = _T("");
		int lower = 0, upper = 0;
		if(tips.FromPos(nchar,lower, upper, keylist) == FALSE)
		return;

		keylist.Replace(_T(','),_T(';'));
		keylist.Replace(_T("and"),_T(";"));

		 // parse the verse list
	    sword::VerseKey vk;
	    sword::ListKey list = vk.ParseVerseList( (CStringA) keylist, vk , TRUE);
	
	
    // this string contains the RTF for the tooltip that would be displayed
	CString tip = font.GetHeaderRTF();

	sword::VerseKey key = "";
	
	if(CSwordBibleDoc::pXrefModuleActive == NULL) return;

			
  CString fs;
	fs.Format(_T("\\fs%d "),22);
	tip += fs;
	tip += _T("\\b (") + (CString) CSwordBibleDoc::pXrefModuleActive->Description() + _T(") \\b0\\par\\par");
			
	// iterate through the list of verses and get the corresponsing text for each verse
	for(list= sword::TOP; !list.Error(); list++)
   {
	   
	 key = (const char*) list;

	 CSwordBibleDoc::pXrefModuleActive->SetKey(key);

	 tip += _T("\\f2\\cf7\\b ") + (CString) CSwordBibleDoc::pXrefModuleActive->KeyText() + _T("\\b0\\cf8 ") 
		 + _T(" ") + (CString)CSwordBibleDoc::pXrefModuleActive->RenderText()  + _T("\\par\\par");
	  
	}

   tip += _T("}");


		CBibleSheet::DisplayReference(_T("Scripture Cross-reference"), tip);

	}

	if (pMsgFilter->msg == WM_RBUTTONDOWN)   OnRightButtonDown();



	*pResult = 0;
}

void CDevotionalPage::OnRightButtonDown()
{

	 CPoint point;                                            
     ::GetCursorPos(&point); //where is the mouse?
     CMenu menu; //lets display out context menu :) 
     DWORD dwSelectionMade;                                       
     VERIFY(menu.LoadMenu(_T("IDR_MENU_DEVOTIONAL")) );  
     CMenu *pmenuPopup = menu.GetSubMenu(0);
     ASSERT(pmenuPopup != NULL);   

	  if(CSwordBibleDoc::devZoom)	pmenuPopup->CheckMenuItem(ID_DEVOTIONAL_ZOOMTEXT,MF_CHECKED); 
	

     dwSelectionMade = pmenuPopup->TrackPopupMenu( (TPM_LEFTALIGN|TPM_LEFTBUTTON|
                                                      TPM_RETURNCMD),
                                                    point.x, point.y, this); 

	

	
	switch (dwSelectionMade)
	{
	case ID_DEVOTIONAL_PRINT:			OnPrint(); break;
	case ID_DEVOTIONAL_COPY:			OnDevotionalCopy(); break;
	case ID_DEVOTIONAL_SELECTALL:       OnSelectAll(); break;
    case ID_DEVOTIONAL_DISPLAY:			OnDevotionalDisplay(); break;
	case ID_DEVOTIONAL_ZOOMTEXT:        OnZoom(); break; 
	case ID_PREVIOUSDATE:				OnPreviousDate(); break;
	case ID_NEXTDATE:					OnNextDate(); break;

	}

	
	pmenuPopup->DestroyMenu(); 



}



void CDevotionalPage::OnSelDDCalendar(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMSELCHANGE pSelChange = reinterpret_cast<LPNMSELCHANGE>(pNMHDR);
	
	CTime time;
    DDCalendarCtrl.GetCurSel(time);         // Get current time

	 // set the key
	CString key;
	month  = time.GetMonth();
	day = time.GetDay();

	if(month > 9 && day > 9)
	key.Format(_T("%d.%d"),month, day);

	else if(month < 10 && day < 10)
	key.Format(_T("0%d.0%d"),month, day);

	else if (month > 9 && day < 10)
	key.Format(_T("%d.0%d"),month, day);

	else if (month < 10 && day > 9)
	key.Format(_T("0%d.%d"),month, day);

	CSwordBibleDoc::devotionKey = (CStringA) key;

	  showText();   // update display




	*pResult = 0;
}

void CDevotionalPage::OnFocusRicheditDate()
{
	m_devotionalRich.SetFocus();
}

BOOL CDevotionalPage::OnSetActive()
{
	showText();
	return CPropertyPage::OnSetActive();
}
